package jetpack.com.pzl;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.room.Room;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import jetpack.com.pzl.architecture.UserInfo;
import jetpack.com.pzl.architecture.UserViewModel;
import jetpack.com.pzl.architecture.data_binding.DataBindingActivity;
import jetpack.com.pzl.architecture.lifecycle.MyLifecycleObserver;
import jetpack.com.pzl.architecture.room.Address;
import jetpack.com.pzl.architecture.room.AppDatabase;
import jetpack.com.pzl.architecture.room.User;
import jetpack.com.pzl.architecture.room.UserDao;

public class AndroidArchitectureComponentActivity extends AppCompatActivity {

    TextView tvArchitectureMsg;
    UserViewModel userModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_architecture_component);

        tvArchitectureMsg = findViewById(R.id.tvArchitectureMsg);
        userModel = ViewModelProviders.of(this).get(UserViewModel.class);

        //让TextView观察ViewModel中数据的变化,并实时展示
        userModel.mUserLiveData.observe(this, new Observer<UserInfo>() {
            @Override
            public void onChanged(UserInfo user) {
                tvArchitectureMsg.setText(user.toString());
            }
        });

        getLifecycle().addObserver(new MyLifecycleObserver());

    }

    //Room
    AppDatabase db;

    public void click(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.btnArchitectureDataBinding:
                intent.setClass(this, DataBindingActivity.class);
                startActivity(intent);
                break;
            case R.id.btnArchitectureViewModel:
                userModel.updateData();
                break;
            case R.id.btnArchitectureViewModelShare:
                break;
            case R.id.btnArchitectureViewModelRoom:
                //https://developer.android.google.cn/training/data-storage/room/

                String dbName = "mn-practice-room";
                db = Room.databaseBuilder(getApplicationContext(),
                        AppDatabase.class, dbName).allowMainThreadQueries().
                        build();
                UserDao userDao = db.userDao();

                User u = new User();
                u.address = new Address();
                u.address.city = "长沙";
                u.address.postCode = 1;
                u.address.state = "良好";
                u.address.street = "东方红";
                u.firstName = "班";
                u.lastName = "超";
                userDao.delete(u);
                userDao.insertAll(u);

                User uu = userDao.findByName("班", "超");
                Toast.makeText(this, uu.toString(), Toast.LENGTH_LONG).show();
                break;
        }
    }


}
